package com.kandroid.designpattern.state.demo1;

import com.kandroid.designpattern.state.demo1.states.*;
import com.kandroid.designpattern.state.demo2.states.BankNoMoneyState;

public class ATM implements ATMAction {
    ATMState atmState = null;
    int cardMoney = 0;
    int inputMoney = 0;

    public ATM() {
        this.cardMoney = 20000;
        changeState(ATMState.STATE_NO_BANKCARD);
    }

    @Override
    public boolean hasEnoughCardMoney() {
        return cardMoney >= inputMoney;
    }

    @Override
    public int getCardMoney() {
        return cardMoney;
    }

    @Override
    public int getInputMoney() {
        return inputMoney;
    }

    @Override
    public void changeState(String state) {
        switch (state) {
            case ATMState.STATE_NO_BANKCARD:
                atmState = new NoBankCardState(this);
                break;
            case ATMState.STATE_HAS_BANKCARD:
                atmState = new HasBankCardState(this);
                break;
            case ATMState.STATE_DECODING:
                atmState = new DecodingState(this);
                break;
            case ATMState.STATE_FETCH_MONEY:
                atmState = new FetchMoneyState(this);
                break;
            case ATMState.STATE_OUT_MONEY:
                atmState = new OutMoneyState(this);
                break;
            case ATMState.STATE_INSUFFICIENT_BALANCE:
                atmState = new InsufficientBalanceState(this);
                break;
        }
    }

    @Override
    public void insertBankCard() {
        atmState.insertBankCard();
    }

    @Override
    public void backBankCard() {
        atmState.backBankCard();
    }

    @Override
    public void inputPassword() {
        atmState.inputPassword();
    }

    @Override
    public void inputMoney(int money) {
        inputMoney = money;
        atmState.inputMoney(money);
    }

    @Override
    public void tapOkButton() {
        atmState.tapOkButton();
    }

    @Override
    public String getName() {
        return "Unknown";
    }

    public void printState() {
        StringBuilder sb = new StringBuilder();
        sb.append("=======ATM状态=======\n");
        sb.append(String.format("当前状态：%s\n", atmState.getName()));
        sb.append(String.format("ATM余额: %s\n", cardMoney));
        sb.append("=======ATM状态-结束=======\n");

        System.out.println(sb.toString());
    }
}
